package com.luochen.cdpt.service;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import com.luochen.cdpt.eneity.*;
import com.luochen.cdpt.repository.*;
import org.springframework.security.authentication.DisabledException;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

@Service
public class CustomUserService implements UserDetailsService {

    @Resource
    private IUserRepository userRepository;

    @Resource
    private IEmpInfoRepository empInfoRepository;

    @Resource
    private IRoleUserRepository roleUserRepository;

    @Resource
    private IRoleMenuRepository roleMenuRepository;

    @Resource
    private IMenuRepository menuRepository;

    @Override
    public UserDetails loadUserByUsername(String username)
            throws UsernameNotFoundException, DisabledException {
        //支持用账号或者手机号码登录
        User user = userRepository.findByUsername(username);
        if (user == null) {
            //用手机号登录
            List<EmpInfo> empInfos = (List<EmpInfo>) empInfoRepository.findAll(QEmpInfo.empInfo.mobilephone.eq(username));
            if (null != empInfos && empInfos.size() > 0) {
                username = empInfos.get(0).getEmpcde();
                user = userRepository.findByUsername(username);
            }
        }
        if (null == user) throw new UsernameNotFoundException("用户不存在");
        if (!user.isEnabled()) throw new DisabledException("用户已被禁用,请联系管理员！");
        getUserRole(user);
        return user;
    }

    /**
     * 获取用户权限
     * @param user
     */
    private void getUserRole(User user) {
        List<RoleUser> roleUsers = (List<RoleUser>)
                roleUserRepository.findAll(QRoleUser.roleUser.userid.eq(user.getId()));
        List<Long> roleIds = new ArrayList<>();
        if (null != roleUsers && roleUsers.size() > 0) {
            roleUsers.forEach(roleUser -> roleIds.add(roleUser.getRoleid()));
            List<RoleMenu> roleMenus =
                    (List<RoleMenu>) roleMenuRepository.findAll(QRoleMenu.roleMenu.roleid.in(roleIds));
            List<Long> menuIds = new ArrayList<>();
            if (null != roleMenus && roleMenus.size() > 0) {
                roleMenus.forEach(roleMenu -> menuIds.add(roleMenu.getMenuid()));
                List<Menu> menus = (List<Menu>) menuRepository.findAllById(menuIds);
                if (null != menus && menus.size() > 0) {
                    List<GrantedAuthority> authorities = new ArrayList<>();
                    //权限里面暂时放菜单
                    menus.forEach(menu -> authorities.add(new SimpleGrantedAuthority(menu.getCode())));
                    user.setAuthorities(authorities);
                }
            }
        }
    }

}
